package pit.com.ordermobilesystem;

import pit.com.ordermobilesystem.adapter.OrderMobileAdapter;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.tabhost.TabGroupActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListAdapter;

public class TitlesFragment extends BaseFragment {
	/**
	 * tiÃªu Ä‘á»� Fragment Thong Nguyen Catch notification from content
	 * provider at step 8' option A
	 * 
	 */


	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		Cursor data = getActivity().getContentResolver().query(
				OrderMobileProvider.CATEGORY_CONTENT_URI, null, null, null,
				null);

		ExpandableListView lv = (ExpandableListView) getActivity()
				.findViewById(R.id.expand_list);
		ListAdapter adapter = lv.getAdapter();


		if (adapter == null) {
			OrderMobileAdapter mobileadapter = new OrderMobileAdapter(data,
					getActivity());
			lv.setAdapter(mobileadapter);
		} else {
			OrderMobileAdapter mobileadapter = (OrderMobileAdapter) adapter;
			mobileadapter.changeCursor(data);
			mobileadapter.notifyDataSetChanged();
		}

		lv.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Item item = (Item) v.getTag();

					DetailsFragment frag = new DetailsFragment();
					// DetailsFragment.bt_chon.setVisibility(View.VISIBLE);
					frag.setItem(item);
					FragmentTransaction transaction = getFragmentManager()
							.beginTransaction();
					transaction.replace(R.id.details_frag, frag);
					transaction.addToBackStack(null);
					transaction.commit();



				return false;
			}


		});

	}

	protected TabGroupActivity getParent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.titlesfragment, container,
				false);
		return rootView;
	}


}
